<?php

namespace app\api\controller\agent;

use app\api\model\Wxapp as WxappModel;
use app\api\controller\Controller;
use app\api\model\Order as OrderModel;
use app\api\model\User;
use app\common\exception\BaseException;
use app\common\library\helper;
use app\common\library\wechat\WxPay;
use think\Db;
use think\Log;
use app\api\model\Transfers as TransfersModel;

/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        // 订单总数
        $model = new OrderModel;
        $orderCount = [
            'payment' => $model->getAgentCount($userInfo['user_id'], 'payment'),
            'delivery' => $model->getAgentCount($userInfo['user_id'], 'delivery'),
            'received' => $model->getAgentCount($userInfo['user_id'], 'received'),
        ];
        $userModel = new User();
        if ($userInfo->agent_level < 4) {
            $userInfo['agent'] = helper::getAgentInfo($userInfo);
            $userInfo['agent_count'] = $userModel->getMyAgentCount($userInfo);
            $userInfo['team'] = $userModel->getMyTeam($userInfo);
        }
        return $this->renderSuccess(compact('userInfo', 'orderCount'));
    }

    /**
     * 提现接口
     */
    public function transfers()
    {
//        return $this->renderError('接口维护中');
        Log::record('开始提现');
        $money = input('money');
        if (!is_numeric($money)) {
            return $this->renderError('请输入数字');
        }
        Log::record('提现金额' . $money);
        $userInfo = $this->getUser();
        //检查余额是否充足
        if ($money > $userInfo->balance) {
            return $this->renderError('余额不足');
        }
        Log::record('用户:' . json_encode($userInfo));
        $wxConfig = WxappModel::getWxappCache(input('wxapp_id'));
        $WxPay = new WxPay($wxConfig);
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $trade_no = 'TX' . date('YmdHis') . rand(1000, 9999)
            . substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        Log::record('流水号：' . $trade_no);
        Db::startTrans();
        //冻结提现金额
        $userInfo->frozen = $money;
        $userInfo->balance = $userInfo->balance - $money;
        $userInfo->save();
        //提现记录
        $transfers = [
            'user_id' => $userInfo->user_id,
            'trade_no' => $trade_no,
            'status' => 'PROCESSING',
            'create_time' => time(),
            'amount' => $money,
	    'detail_id'=>''
    ];
	$trans = new TransfersModel();
        $trans->save($transfers);
        //调用企业付款到零钱接口
        try {
            $res = $WxPay->transfers($userInfo->open_id, $money, $trade_no);
            if ($res['result_code'] == 'FAIL') {
                Db::rollback();
                return $this->renderError($res['return_msg']);
            }
        } catch (BaseException $e) {
            $userInfo->frozen = $userInfo->frozen - $money;
            $userInfo->balance = $userInfo->balance + $money;
            Db::rollback();
            return $this->renderError($e->getMessage());
        }
        $userInfo->frozen = $userInfo->frozen - $money;
        $userInfo->save();
        Db::commit();
        return $this->renderSuccess();
    }
}
